package com.cloudlibrary.service.impl;


import com.cloudlibrary.domain.Book;
import com.cloudlibrary.entity.PageResult;
import com.cloudlibrary.service.BookService;
import com.cloudlibrary.mapper.BookMapper;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
* @author Guan
* @description 针对表【book】的数据库操作Service实现
* @createDate 2024-12-19 14:52:23
*/
@Service
@Transactional
public class BookServiceImpl implements BookService {
    @Autowired
private BookMapper bookMapper;

    @Override
    public Book findBooKById(String id) {
        return bookMapper.findById(id);
    }

    @Override
    public Integer borrowBook(Book book) {
        Book b=this.findBooKById(book.getId()+"");
        DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        book.setBorrowtime(dateFormat.format(new Date()));
        book.setStatus("1");
        book.setUploadtime(b.getUploadtime());
        return bookMapper.editBook(book);
    }

    @Override
    public PageResult selectNewBooks(Integer pageNum,Integer pageSize){
        PageHelper.startPage(pageNum,pageSize);
        Page<Book>page = bookMapper.selectNewBooks();
        return new PageResult(page.getTotal(),page.getResult());
    }

    @Override
    public PageResult search(Book book, Integer pageNum, Integer pageSize) {
        return null;
    }

    @Override
    public Integer addBook(Book book) {
        DateFormat dateFormat=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        book.setUploadtime(dateFormat.format(new Date()));
        return bookMapper.addBook(book);
    }
}




